var tool = require('../../tool');
var models = require('../../models');
module.exports = function(req, res, next) {
    var methods = {
        'get' : function(req, render) {
            render({});
        },
        'post' : function(req, render) {
            var body = req.body;
            var userModel = models['user'];
            var model = new userModel();
            if(!body['headpic']){
                body['headpic'] = 'http://www.gravatar.com/avatar/' + tool.md5(body['mail']);
            }
            body['password'] = tool.md5(body['password']);
            for (var key in body) {
                model[key] = body[key];
            }
            model.save(function(err, doc) {
                if (err) {
                    console.log(err);
                    return;
                }
                render(doc);
            });
        }
    };
    var methodName = req.method.toLowerCase();
    var render = function(data) {
        res.render('user/add', {
            'title' : 'Show This',
            'data' : data
        }, 0);
    };    
    methods[methodName](req, render);
};
